//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;
using Tathata.Common.DAL.Managers;
using Tathata.Common.BLL.Enums;

namespace Tathata.Common.BLL.Managers
{
    /// <summary>
    /// Message campaign manager
    /// </summary>
    public class CampaignManager
    {
        #region Methods
        /// <summary>
        /// Gets a campaign by campaign identifier
        /// </summary>
        /// <param name="CampaignID">Campaign identifier</param>
        /// <returns>Message template</returns>
        public static Campaign GetByCampaignID(int CampaignID)
        {
            return CampaignDBManager.GetByCampaignID(CampaignID);
        }

        /// <summary>
        /// Deletes a campaign
        /// </summary>
        /// <param name="CampaignID">Campaign identifier</param>
        public static void DeleteCampaign(int CampaignID)
        {
            CampaignDBManager.DeleteCampaign(CampaignID);
        }

        /// <summary>
        /// Gets all campaigns
        /// </summary>
        /// <returns>Campaign collection</returns>
        public static CampaignCollection GetAllCampaigns()
        {
            return CampaignDBManager.GetAllCampaigns();
        }

        /// <summary>
        /// Inserts a campaign
        /// </summary>
        /// <param name="Name">The name</param>
        /// <param name="Subject">The subject</param>
        /// <param name="Body">The body</param>
        /// <param name="CreatedOn">The date and time of instance creation</param>
        /// <returns>Campaign</returns>
        public static Campaign InsertCampaign(string Name, string Subject, string Body, DateTime CreatedOn)
        {
            return CampaignDBManager.InsertCampaign(Name, Subject, Body, CreatedOn);
        }

        /// <summary>
        /// Updates the campaign
        /// </summary>
        /// <param name="CampaignID">The campaign identifier</param>
        /// <param name="Name">The name</param>
        /// <param name="Subject">The subject</param>
        /// <param name="Body">The body</param>
        /// <param name="CreatedOn">The date and time of instance creation</param>
        /// <returns>Campaign</returns>
        public static Campaign UpdateCampaign(int CampaignID,
           string Name, string Subject, string Body, DateTime CreatedOn)
        {
            return CampaignDBManager.UpdateCampaign(CampaignID, Name, Subject, Body, CreatedOn);
        }

        /// <summary>
        /// Sends a campaign to specified emails
        /// </summary>
        /// <param name="CampaignID">Campaign identifier</param>
        /// <param name="Customers">Customers</param>
        /// <returns>Total emails sent</returns>
        public static int SendCampaign(int CampaignID, CustomerCollection Customers)
        {
            int totalEmailsSent = 0;
            Campaign campaign = GetByCampaignID(CampaignID);
            if (campaign == null)
                throw new Exception("Campaign could not be loaded");
            foreach (Customer customer in Customers)
            {
                if (!customer.Active)
                    continue;
                List<string> to = new List<string>();
                to.Add(customer.Email);
                MessageManager.SendEmail(campaign.Subject, campaign.Body, MessageManager.AdminEmailAddress, to);
                totalEmailsSent++;
            }
            return totalEmailsSent;
        }

        /// <summary>
        /// Sends a campaign to specified email
        /// </summary>
        /// <param name="CampaignID">Campaign identifier</param>
        /// <param name="Email">Email</param>
        public static void SendCampaign(int CampaignID, string Email)
        {
            Campaign campaign = GetByCampaignID(CampaignID);
            if (campaign == null)
                throw new Exception("Campaign could not be loaded");
            List<string> to = new List<string>();
            to.Add(Email);
            MessageManager.SendEmail(campaign.Subject, campaign.Body, MessageManager.AdminEmailAddress, to);

        }
        #endregion
    }
}
